package cn.lili.modules.payment.entity.dos;


import cn.lili.common.utils.GsonUtils;
import cn.lili.modules.payment.entity.dto.OutOrderItem;
import cn.lili.modules.payment.wechat.model.profitsharing.ProfitSharingResponse;
import cn.lili.mybatis.model.BaseEntity;
import cn.lili.mybatis.model.BaseStandardEntity;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

@Data
@TableName("li_out_order_log")
@ApiModel(value = "分账记录")
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class OutOrderLog extends BaseStandardEntity {

    /**
     * 商城系统中的订单编号
     */
    @ApiModelProperty(value = "订单单号")
    private String orderSn;


    @ApiModelProperty(value = "支付方式")
    private String paymentMethod;

    /**
     * 分账出资的电商平台二级商户，
     * 填写微信支付分配的商户号。
     */
    @ApiModelProperty(value = "二级商户号")
    private String subMchid;

    @ApiModelProperty(value = "微信订单号-微信支付订单号")
    private String transactionId;

    /**
     * 商户系统内部的分账单号，
     * 在商户系统内部唯一（单次分账、多次分账、完结分账应使用不同的商户分账单号），
     * 同一分账单号多次请求等同一次。
     */
    @ApiModelProperty(value = "商户分账单号")
    private String outOrderNo;

    @ApiModelProperty(value = "分账金额")
    private Double amount;

    /**
     * 微信分账单号，微信支付系统返回的唯一标识
     */
    @ApiModelProperty(value = "微信分账单号")
    private String orderId;

    /**
     * PROCESSING：处理中
     * FINISHED：分账完成
     */
    @ApiModelProperty(value = "分账单状态")
    private String status;

    @ApiModelProperty(value = "分账详情")
    private String detail;

    public void setDetail(List<OutOrderItem> outOrderItems) {
        this.detail = GsonUtils.toJson(outOrderItems);
    }

    public void getDetailItems() {
        if (this.detail == null) {
            return;
        }
        GsonUtils.fromJson(this.detail, OutOrderItem.class);
    }

    public OutOrderLog(String orderSn, Double amount, ProfitSharingResponse profitSharingResponse) {
        this.orderSn = orderSn;
        this.subMchid = profitSharingResponse.getSub_mchid();
        this.transactionId = profitSharingResponse.getTransaction_id();
        this.outOrderNo = profitSharingResponse.getOut_order_no();
        this.amount = amount;
        this.orderId = profitSharingResponse.getOrder_id();
        this.status = profitSharingResponse.getStatus();
    }
}
